package com.project.service;

import com.project.dao.EmploymentInfoMapper;
import com.project.entity.EmploymentInfo;
import com.project.myBeans.EmploymentInfoDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by hasee on 2017/4/28.
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class EmploymentInfoServiceImpl extends BaseService implements EmploymentInfoService {

    @Autowired
    private UserService userService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private StudentInfoService studentInfoService;

    private static EmploymentInfoMapper employmentInfoMapper = null;

    public void setMapper() {
        if (employmentInfoMapper == null)
            employmentInfoMapper = sqlSessionTemplate.getMapper(EmploymentInfoMapper.class);//1.获取相关mapper
    }

    @Override
    public EmploymentInfo findById(int id) {
        setMapper();
        return employmentInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(EmploymentInfo employmentInfo) {
        setMapper();
        return employmentInfoMapper.updateByPrimaryKeySelective(employmentInfo);
    }

    @Override
    public int add(EmploymentInfo employmentInfo) {
        setMapper();
        return employmentInfoMapper.insertSelective(employmentInfo);
    }

    @Override
    public int delete(int id) {
        setMapper();
        return employmentInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<EmploymentInfo> findall() {
        setMapper();
        return employmentInfoMapper.selectAll();
    }

    @Override
    public List<EmploymentInfo> findByCondition(EmploymentInfo employmentInfo) {
        setMapper();
        return employmentInfoMapper.selectByCondition(employmentInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/7/2
     * function: 根据id查找就业信息详情
     *
     * @param id
     * @return
     */
    @Override
    public EmploymentInfoDetail findDetailById(Integer id) throws Exception {
        EmploymentInfoDetail employmentInfoDetail = new EmploymentInfoDetail();
        //设置就业信息
        EmploymentInfo employmentInfo = findById(id);
        employmentInfoDetail.setEmploymentInfo(employmentInfo);
        if (employmentInfo != null) {
            //设置学生详细信息
            if (employmentInfo.getUserid() != null)
                employmentInfoDetail.setStudentInfoDetail(studentInfoService.findDetailByUserId(employmentInfo.getUserid()));

            //设置单位信息
            if (employmentInfo.getEmployerid() != null)
                employmentInfoDetail.setOrgInfo(orgInfoService.findByid(employmentInfo.getEmployerid()));
        }
        return employmentInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/7/19
     * function: 根据userId更新该用户的就业信息
     *
     * @param employmentInfo
     * @return
     */
    @Override
    public int updateBatchByUserId(EmploymentInfo employmentInfo) {
        setMapper();
        return employmentInfoMapper.updateBatchByUserIdSelective(employmentInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/8/6
     * function: 根据用户id查询有效（valid=1）的就业信息
     *
     * @param userId
     * @return
     */
    @Override
    public EmploymentInfo findOneByUserAndValid(Integer userId) {
        setMapper();
        return employmentInfoMapper.selectOneByUserAndValid(userId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/19
     * function: 根据学生用户表idList批量删除
     *
     * @param userIdList
     * @return
     */
    @Override
    public int removeBatchByStudentInfoIdList(List userIdList) {
        setMapper();
        return employmentInfoMapper.deleteBatchByStudentInfoIdList(userIdList);
    }


}
